package com.http.intg;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

@Configuration
public class HttpServiceConfig {
    public static final Logger log = LoggerFactory.getLogger(HttpServiceConfig.class);

    @Value("${com.http.link:}")
    private String httpUrl;

    @Value("${com.http.connect-timeout:}")
    private int connectTimeout;

    @Value("${com.http.read-timeout:}")
    private int readTimeout;

    //Use restTemplateBuilder prepared by Spring Boot
    @Bean
    public HttpServiceClient httpServiceClient(final RestTemplateBuilder restTemplateBuilder){
        System.out.println("Test httpUrl:"+this.httpUrl);
        System.out.println("Test connectTimeout:"+this.connectTimeout);
        System.out.println("Test readTimeout:"+this.readTimeout);
        RestTemplate restTemplate = restTemplateBuilder.rootUri(this.httpUrl)
                .setReadTimeout(Duration.ofMillis(this.readTimeout))
                .setConnectTimeout(Duration.ofMillis(this.connectTimeout))
                .build();
        HttpServiceClient httpServiceClient = new HttpServiceClient();
        httpServiceClient.setRestTemplate(restTemplate);
        return httpServiceClient;
    }
}
